package cn.yoyo.service;

import cn.yoyo.entity.LargeFile;
import cn.yoyo.entity.SysUser;
import cn.yoyo.mapper.LargeFileMapper;
import cn.yoyo.mapper.UserMapper;
import cn.yoyo.tools.GetUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired(required = false)
    LargeFileMapper largeFileMapper;

    @Override
    public List<SysUser> findAll(Integer start, Integer num) {
        // 当前页数和每页和每页个数不能小于1
        if (start < 1 || num < 1) return null;
        Integer pageNow = 1;
        if (start > 0) {
            pageNow = (start - 1) * num;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("start", pageNow);
        map.put("num", num);
        return userMapper.findAll(map);
    }

    @Override
    public int freeze(Integer closed, Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("closed", closed);
        map.put("id", id);
        return userMapper.freeze(map);
    }

    @Override
    public int getTotal() {
        return userMapper.getTotal();
    }

    @Override
    public int addUser(SysUser user, MultipartFile file) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        if (file != null && file.getSize() > 0) {
            user.setImg(UUID.randomUUID().toString());
        }
        return userMapper.addUser(user);
    }

    @Override
    public int updatePassword(String password) {
        SysUser user = GetUser.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("password", passwordEncoder.encode(password));
        map.put("id", user.getId());
        return userMapper.updatePassword(map);
    }

    @Override
    @Transactional
    public int updateImg(MultipartFile file) {
        SysUser user = GetUser.getUser();
        String picture = user.getImg();// 先把用户的旧头像取出来
        String uuid = UUID.randomUUID().toString();
        try {
            largeFileMapper.add(new LargeFile(uuid, file.getOriginalFilename(), file.getBytes()));
            if (picture != null && !("".equals(picture))) {
                largeFileMapper.delete(picture);// 更换新的头像后把旧的头像删除
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("img", uuid);
        map.put("id", user.getId());
        return userMapper.updateImg(map);
    }

    @Override
    public int updateInfo(String nickname, String phone) {
        SysUser user = GetUser.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("nickname", nickname);
        map.put("phone", phone);
        map.put("id", user.getId());
        return userMapper.updateInfo(map);
    }

    @Override
    public int addAdm(SysUser user) {
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setRole("G");
        return userMapper.addAdm(user);
    }

    @Override
    public int resetPassword(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("password", passwordEncoder.encode("111111"));
        map.put("closed", 0);
        map.put("id", id);
        return userMapper.resetPassword(map);
    }

    @Override
    public int findById(Integer id) {
        return userMapper.findById(id);
    }
}
